package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.db.domain.system.PcComboPro;
import com.ruoyi.db.domain.system.PcProCombo;
import com.ruoyi.db.domain.system.PcProduct;
import com.ruoyi.db.mapper.system.PcComboProMapper;
import com.ruoyi.db.mapper.system.PcProComboMapper;
import com.ruoyi.db.mapper.system.PcProductMapper;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IPcProComboService;

import javax.transaction.Transactional;

/**
 * 【产品套餐】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-23
 */
@Service
public class PcProComboServiceImpl implements IPcProComboService
{
    @Autowired
    private PcProComboMapper pcProComboMapper;
    @Autowired
    private PcProductMapper pcProductMapper;
    @Autowired
    private PcComboProMapper pcComboProMapper;

    /**
     * 查询【产品套餐】
     * 
     * @param proComboId 【产品套餐】主键
     * @return 【产品套餐】
     */
    @Override
    public PcProCombo selectPcProComboByProComboId(Long proComboId)
    {
        return pcProComboMapper.selectPcProComboByProComboId(proComboId);
    }

    /**
     * 查询【产品套餐】列表
     * 
     * @param pcProCombo 【产品套餐】
     * @return 【产品套餐】
     */
    @Override
    public List<PcProCombo> selectPcProComboList(PcProCombo pcProCombo)
    {
        List<PcProCombo> combos = pcProComboMapper.selectPcProComboList(pcProCombo);
        for (PcProCombo combo: combos) {
            PcComboPro pcComboPro = new PcComboPro();
            pcComboPro.setProComboId(combo.getProComboId());
            List<PcComboPro> cpList = pcComboProMapper.selectPcComboProList(pcComboPro);
            List<PcProduct> products = new ArrayList<>();
            BigDecimal proAllAmount = new BigDecimal("0");
            for (PcComboPro cp: cpList) {
                PcProduct pcProduct = pcProductMapper.selectPcProductByProId(cp.getProId());
                pcProduct.setComboAmount(cp.getComboAmount());
                products.add(pcProduct);
                proAllAmount=proAllAmount.add(pcProduct.getRmbPrice().multiply(new BigDecimal(cp.getComboAmount())));
            }
            combo.setTotalProAmount(proAllAmount);
            combo.setProducts(products);
        }
        return combos;
    }

    /**
     * 新增【产品套餐】
     * 
     * @param pcProCombo 【产品套餐】
     * @return 结果
     */
    @Override
    public int insertPcProCombo(PcProCombo pcProCombo)
    {
        pcProCombo.setIsDelete(0);
        pcProCombo.setSellStatus(1l);
        pcProCombo.setCreateTime(DateUtils.getNowDate());
        pcProCombo.setSellAmount(0l);
        if(pcProComboMapper.insertPcProCombo(pcProCombo)>0){
            List<PcProduct> pros =  pcProCombo.getProducts();
            for (PcProduct product : pros){
                PcComboPro pcComboPro = new PcComboPro();
                pcComboPro.setProId(product.getProId());
                if(product.getComboAmount()==null){
                    pcComboPro.setComboAmount(0l);
                }else{
                    pcComboPro.setComboAmount(product.getComboAmount());
                }
                pcComboPro.setProComboId(pcProCombo.getProComboId());
                pcComboProMapper.insertPcComboPro(pcComboPro);
            }
            return 1;
        }
        return 0;
    }

    /**
     * 修改【产品套餐】
     * 
     * @param pcProCombo 【产品套餐】
     * @return 结果
     */
    @Override
    public int updatePcProCombo(PcProCombo pcProCombo)
    {
        if(pcProComboMapper.updatePcProCombo(pcProCombo)>0){
            //删除套餐产品关系表产品
            pcComboProMapper.deletePcComboProByComBoId(pcProCombo.getProComboId());

            List<PcProduct> pros =  pcProCombo.getProducts();
            for (PcProduct pro : pros){
                PcComboPro pcComboPro = new PcComboPro();
                pcComboPro.setProId(pro.getProId());
                pcComboPro.setComboAmount(pro.getComboAmount());
                pcComboPro.setProComboId(pcProCombo.getProComboId());
                pcComboProMapper.insertPcComboPro(pcComboPro);
            }
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 批量删除【产品套餐】
     * 
     * @param proComboIds 需要删除的【产品套餐】主键
     * @return 结果
     */
    @Override
    public int deletePcProComboByProComboIds(Long[] proComboIds)
    {
        return pcProComboMapper.deletePcProComboByProComboIds(proComboIds);
    }

    /**
     * 删除【产品套餐】信息
     * 
     * @param proComboId 【产品套餐】主键
     * @return 结果
     */
    @Override
    public int deletePcProComboByProComboId(Long proComboId)
    {
        return pcProComboMapper.deletePcProComboByProComboId(proComboId);
    }
}
